{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 部署准备"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install --upgrade git+https://github.com/autodeployai/daas-client.git"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from daas_client import DaasClient\n",
    "client = DaasClient('https://192.168.64.3:30931', 'username', 'password')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "project = '部署测试'\n",
    "if not client.project_exists(project):\n",
    "        client.create_project(project, 'deployment-test', '部署测试项目')\n",
    "client.set_project(project)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import datasets\n",
    "from sklearn.model_selection import train_test_split\n",
    "import pandas as pd\n",
    "from pprint import pprint\n",
    "import requests\n",
    "\n",
    "seed = 123456\n",
    "\n",
    "iris = datasets.load_iris()\n",
    "iris_target_name = 'Species'\n",
    "iris_feature_names = iris.feature_names\n",
    "iris_df = pd.DataFrame(iris.data, columns=iris_feature_names)\n",
    "iris_df[iris_target_name] = iris.target\n",
    "\n",
    "X, y = iris_df[iris_feature_names], iris_df[iris_target_name]\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=seed)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 部署Scikit-learn模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.svm import SVC\n",
    "\n",
    "model = SVC(probability=True, random_state=seed)\n",
    "model.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "publish_resp = client.publish(model,\n",
    "                              name='iris',\n",
    "                              mining_function='classification',\n",
    "                              X_test=X_test,\n",
    "                              y_test=y_test,\n",
    "                              description='A SVC model')\n",
    "pprint(publish_resp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_resp = client.test(publish_resp['model_name'], model_version=publish_resp['model_version'])\n",
    "pprint(test_resp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = requests.post(test_resp['endpoint_url'],\n",
    "                         headers={'Authorization': 'Bearer {token}'.format(token=test_resp['access_token'])},\n",
    "                         json=test_resp['payload'],\n",
    "                         verify=False)\n",
    "pprint(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "deploy_resp = client.deploy(model_name='iris', \n",
    "                            deployment_name='iris-svc',\n",
    "                            model_version=publish_resp['model_version'],\n",
    "                            replicas=1)\n",
    "pprint(deploy_resp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = requests.post(deploy_resp['endpoint_url'],\n",
    "                         headers={'Authorization': 'Bearer {token}'.format(token=deploy_resp['access_token'])},\n",
    "                         json=deploy_resp['payload'],\n",
    "                         verify=False)\n",
    "pprint(response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 部署XGBoost模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from xgboost import XGBClassifier\n",
    "\n",
    "model = XGBClassifier(max_depth=3, objective='multi:softprob', random_state=seed)\n",
    "model = model.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "publish_resp = client.publish(model,\n",
    "                              name='iris',\n",
    "                              mining_function='classification',\n",
    "                              X_test=X_test,\n",
    "                              y_test=y_test,\n",
    "                              description='A XGBClassifier model')\n",
    "pprint(publish_resp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_resp = client.test(publish_resp['model_name'], model_version=publish_resp['model_version'])\n",
    "pprint(test_resp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = requests.post(test_resp['endpoint_url'],\n",
    "                         headers={'Authorization': 'Bearer {token}'.format(token=test_resp['access_token'])},\n",
    "                         json=test_resp['payload'],\n",
    "                         verify=False)\n",
    "pprint(response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 部署LightGBM模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from lightgbm import LGBMClassifier\n",
    "\n",
    "model = LGBMClassifier()\n",
    "model = model.fit(X_train, y_train, eval_set=[(X_test, y_test)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "publish_resp = client.publish(model,\n",
    "                              'iris',\n",
    "                              mining_function='classification',\n",
    "                              X_test=X_test,\n",
    "                              y_test=y_test,\n",
    "                              description='A LGBMClassifier model')\n",
    "pprint(publish_resp)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 部署PySpark模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyspark.sql import SparkSession\n",
    "from pyspark.ml.classification import RandomForestClassifier\n",
    "from pyspark.ml.feature import VectorAssembler\n",
    "from pyspark.ml import Pipeline\n",
    "\n",
    "spark = SparkSession.builder.getOrCreate()\n",
    "df = spark.createDataFrame(iris_df)\n",
    "\n",
    "df_train, df_test = df.randomSplit([0.7, 0.3], seed=seed)\n",
    "assembler = VectorAssembler(inputCols=iris_feature_names,\n",
    "                            outputCol='features')\n",
    "\n",
    "rf = RandomForestClassifier(seed=seed).setLabelCol(iris_target_name)\n",
    "pipe = Pipeline(stages=[assembler, rf])\n",
    "model = pipe.fit(df_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "publish_resp = client.publish(model,\n",
    "                              name='iris',\n",
    "                              mining_function='classification',\n",
    "                              data_test=df_test,\n",
    "                              description='A RandomForestClassifier of Spark model')\n",
    "pprint(publish_resp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
